package org.example.service.impl;

import com.github.javafaker.Faker;
import org.example.mapper.IOCMapper;
import org.example.model.invoices_f;
import org.example.model.share_f;
import org.example.service.IOCInsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class IOCInsertServiceImpl implements IOCInsertService {

    final IOCMapper iocMapper;

    @Autowired
    public IOCInsertServiceImpl(IOCMapper iocMapper) {
        this.iocMapper = iocMapper;
    }

    @Override
    public int insert() {
        Map<String,String> map = new HashMap<>();
        //        String citys[] = {"北","广","山","苏","南","海","北","江","港","西","西","南","庆","建","津","南","川","西","徽","海","西","北",,"辽宁","内蒙古"};
        map.put("北京","20");
        map.put("广东","21");
        map.put("山东","22");
        map.put("江苏","23");
        map.put("河南","25");
        map.put("上海","26");
        map.put("河北","27");
        map.put("浙江","24");
        map.put("香港","29");
        map.put("山西","30");
        map.put("陕西","31");
        map.put("湖南","12");
        map.put("重庆","33");
        map.put("福建","28");
        map.put("天津","34");
        map.put("云南","35");
        map.put("四川","36");
        map.put("广西","37");
        map.put("安徽","38");
        map.put("海南","39");
        map.put("江西","40");
        map.put("湖北","41");
        map.put("辽宁","42");
        map.put("内蒙古","43");
        Map<String,String> Inmap = new HashMap<>();
        //String[] array = {"制造业", "政府", "运营商", "金融", "服务"};
        Inmap.put("制造业","电子半导体制造");
        Inmap.put("政府","监察部");
        Inmap.put("运营商","运营商");
        Inmap.put("金融","银行");
        Inmap.put("服务","专业服务");

        Map<String,String> Inermap = new HashMap<>();
        //String[] array = {"制造业", "政府", "运营商", "金融", "服务"};
        Inermap.put("电子半导体制造","制造业");
        Inermap.put("监察部","政府");
        Inermap.put("运营商","运营商");
        Inermap.put("银行","金融");
        Inermap.put("专业服务","服务");
        //Inermap.get(Inmap.get(Ins1))



        Map<String,String> InInmap = new HashMap<>();
        //String[] array = {"制造业", "政府", "运营商", "金融", "服务"};
        Inmap.put("电子半导体制造","电子半导体制造业");
        Inmap.put("监察部","公检法司");
        Inmap.put("运营商","联通");
        Inmap.put("银行","城市商业银行/农信社");
        Inmap.put("专业服务","IT服务");

        //String citys[] = {"政企","私企","科技公司","技术公司","数字公司","产业公司","产品公司"};
        Map<String,String> Zmap = new HashMap<>();
        Zmap.put("政府","1003");
        Zmap.put("制造业","2004");
        Zmap.put("运营商","3005");
        Zmap.put("金融","4006");
        Zmap.put("服务","5007");

/*-------------------------------------------------------------------------------*/
        Map<String, String> industryMapping = new HashMap<>();
        industryMapping.put("制造业", "行业线");
        industryMapping.put("服务", "互联网");
        industryMapping.put("运营商", "运营商");
        industryMapping.put("金融", "金融");
        industryMapping.put("政府", "政企");

        Map<String, String[]> subIndustries = new HashMap<>();
        subIndustries.put("政企", new String[]{"政企一部", "政企二部"});
 /*----------------------------------------------------------------------------------------------*/


        List<invoices_f> invoicesFs = new ArrayList<>();
        List<share_f> shareFs = new ArrayList<>();
        List<String> names = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();

        for(int i = 0;i<400;i++){
            String name = genertFackName();
            String txt = genertccor(name);
            names.add(name);
            map1.put(name,txt);
        }
        for(int i = 0;i<400;i++) {

            BigDecimal bigDecimal = new BigDecimal(genertUnNum(5));
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            invoices_f invoicesF = new invoices_f();
            share_f shareF = new share_f();


            invoicesF.setInvoicesKey(bigDecimal);
            shareF.setIncomeSecKey(bigDecimal);

            String s = genertPeriodId();
            invoicesF.setPeriodId(s);
            shareF.setPeriodId(s);


            String s2 = genertProvience(true);

            invoicesF.setBusinessUnitL1CnName(s2);
            shareF.setBusinessUnitL1CnName(s2);

            String stt = s2.substring(0,2);
        //-----------------------------------------------
            String s3 = BUcode(map.get(stt));
            invoicesF.setBusinessUnitL2Num(s3);
            shareF.setBusinessUnitL2Num(s3);

            invoicesF.setBusinessUnitL1Num(s3);
            shareF.setBusinessUnitL1Num(s3);
//-----------------------------------------------------

            invoicesF.setBusinessUnitL2CnName(s2.substring(0,2));
            shareF.setBusinessUnitL2CnName(s2.substring(0,2));

            String s5 = genertSalesContractNo();
            invoicesF.setSalesContractNo(s5);
            shareF.setSaleUserJeSourceName(s5);

            invoicesF.setSalesContractName(null);
            shareF.setContractName(null);

            invoicesF.setLevel0IndustryCode(null);
            shareF.setLevel0IndustryCode("Z3");

            invoicesF.setLevel0IndustryCnName(null);

            String Ins1 = genertIndustryCnName();
            shareF.setLevel0IndustryCnName(Ins1);

            invoicesF.setLevel2IndustryCode(null);



            invoicesF.setLevel2IndustryCnName(null);
            shareF.setLevel2IndustryCnName(Inmap.get(Ins1));

            invoicesF.setDistributionBusiness(null);
            shareF.setDistributionBusiness(null);

            invoicesF.setNaLevelName(null);
            shareF.setNaLevelName(genertNaName());

            String s6 = genertCustomerCode();
            invoicesF.setCustomerCode(s6);
            shareF.setCustomerNumber(s6);

            String s7 = generBusinessName();
            invoicesF.setCustomerName(s7);
            shareF.setCustomerName(s7);

            invoicesF.setEndCustomerCode(null);


            invoicesF.setEndCustomerName(null);
            shareF.setAlCustName(generBusinessName_share());

            Random random = new Random();
            String name = names.get(random.nextInt(names.size()));
            System.out.println(name);
            String ccor = map1.get(name);
            System.out.println(ccor);
            //String ccor = genertccor(name);
//            String name = genertFackName();
//            String ccor = genertccor(name);
            //如果sales_business_by这个列里面的数据不需要人的工号的话，下面的第一个set就set为name，如果需要就set为ccor
            invoicesF.setSalesBusinessBy(ccor);
            shareF.setCcorpcontactusername(ccor);

            String s8 = genertAccatCode();
            invoicesF.setLv1ReportAcctCode(s8);
            shareF.setLevel1ReportItemCode(s8);

            invoicesF.setLv1ReportAcctName("回款");
            shareF.setLevel1ReportItemNameCn("净销售收入");

            String s9 = genertAccatCode();
            invoicesF.setLv2ReportAcctCode(s9);
            shareF.setLevel2ReportItemCode(s9);

            invoicesF.setLv2ReportAcctName("回款");
            shareF.setLevel2ReportItemNameCn("服务收入");

            invoicesF.setLv3ReportAcctCode(genertAccatCode());
            shareF.setReportItemCode(null);

            invoicesF.setLv3ReportAcctName("回款");
            shareF.setLevel3ReportItemNameCn(null);
//--------------------------------------------------------------------------
            BigDecimal bigDecimal1 = genertNum();
            BigDecimal bigDecimal2 = genertNum();
            BigDecimal bigDecimal1_1 = new BigDecimal(String.valueOf(bigDecimal1));

            // 正数
            if (bigDecimal1.compareTo(BigDecimal.ZERO) > 0) {
                bigDecimal1_1 = bigDecimal1.add(bigDecimal1);
            }
            // 负数
            else if (bigDecimal1.compareTo(BigDecimal.ZERO) < 0) {
                bigDecimal1_1 = bigDecimal1.add(bigDecimal1).add(bigDecimal1).add(bigDecimal1);
            }
            BigDecimal bigDecimal2_1 = new BigDecimal(String.valueOf(bigDecimal2));

            // 正数
            if (bigDecimal2.compareTo(BigDecimal.ZERO) > 0) {
                bigDecimal2_1 = bigDecimal2.add(bigDecimal2).add(bigDecimal2);
            }
            // 负数
            else if (bigDecimal2.compareTo(BigDecimal.ZERO) < 0) {
                bigDecimal2_1 = bigDecimal2.add(bigDecimal2).add(bigDecimal2).add(bigDecimal2);
            }

            invoicesF.setReportRmb(bigDecimal2);
            shareF.setReportCny(bigDecimal1);

            invoicesF.setReportUsd(bigDecimal2_1);
            shareF.setReportUsd(bigDecimal1_1);
//-------------------------------------------------------------------------------
            invoicesF.setDwLastUpdateDate(timestamp);
            shareF.setDwLastUpdateDate(timestamp);

            BigDecimal bigDecimal11 = genertLettleNum(10);
            invoicesF.setCrtCycleId(bigDecimal11);
            shareF.setCrtCycleId(bigDecimal11);

            invoicesF.setLastUpdCycleId(bigDecimal);
            shareF.setLastUpdCycleId(bigDecimal);

            invoicesF.setLevel1IndustryCode(null);
            shareF.setFinalCustNo(genertindustrycode());

            String genertfinalcode = genertfinalcode(false);
            String s1 = Zmap.get(Ins1);
            //Ins
            shareF.setLevel2IndustryCode("Z300"+s1);
            shareF.setLevel1IndustryCode("Z300"+s1.substring(0,1));

            invoicesF.setLevel1IndustryCnName(null);
            shareF.setLevel1IndustryCnName(Ins1);

            invoicesF.setSalesProjectNo(null);
            shareF.setSalesProjectNo(null);

            invoicesF.setSalesProjectName(null);
            shareF.setSalesProjectName(null);

            invoicesF.setIsOversea("N");
            shareF.setIsOversea("N");

            invoicesF.setReportItemCode(null);
            shareF.setReportItemCode(null);

            invoicesF.setSalesType(null);
            shareF.setSalesType(null);

            invoicesF.setTransType(null);
            shareF.setTransType(null);

            invoicesF.setBusinessNo(null);
            shareF.setBusinessNo(null);

            invoicesF.setSectorNum(null);
            shareF.setSectorNum(null);

            String inputIndustry = Ins1;
            String correspondingIndustry = industryMapping.get(inputIndustry);

            if (correspondingIndustry.equals("政企")) {
                String[] parts = subIndustries.get("政企");
                String randomPart = parts[new Random().nextInt(parts.length)];
                invoicesF.setSectorName(randomPart);
                shareF.setSectorName(randomPart);
//                System.out.println(inputIndustry + "对应的行业是：" + randomPart);
            } else {
                System.out.println(inputIndustry + "对应的行业是：" + correspondingIndustry);
                invoicesF.setSectorName(correspondingIndustry);
                shareF.setSectorName(correspondingIndustry);
            }


            invoicesFs.add(invoicesF);
            shareFs.add(shareF);
        }

        iocMapper.insert(invoicesFs);
        iocMapper.insertShare(shareFs);

        return 1;
    }



    private String genertccor(String name){

        String employeeNumber = "0001"+String.format("%04d",ThreadLocalRandom.current().nextInt(10000));
        String fin = name+" "+employeeNumber;
        return fin;
    }
    /**
     * 使用facker库生成假的名字
     * @return
     */
    private String genertFackName() {
        Faker faker = new Faker(new Locale("zh-CN"));
        String name = faker.name().fullName();
        return name;
    }

    private String genertNaName() {
        Random r = new Random();
        String[] array = {"Non-NA","一般NA","商业NA","战略NA","核心NA"};


        //int randomInt = r.nextInt(10000)%array.length;
        int randomInt = r.nextInt(5);
        String InsName = array[randomInt];
        return InsName;
    }

    private String genertIndustryCnName() {
        Random r = new Random();
        String[] array = {"制造业", "政府", "运营商", "金融", "服务"};

        //int randomInt = r.nextInt(10000)%array.length;
        int randomInt = r.nextInt(5);
        String InsName = array[randomInt];
        return InsName;
    }

    //政企~
    private String genertName(){
        Random r = new Random();
        String citys[] = {"政企","私企","科技公司","技术公司","数字公司","产业公司","产品公司"};
        //int randomInt = r.nextInt(10000)%citys.length;
        int randomInt = r.nextInt(7);
        String businessName = citys[randomInt]+randowText(3);
        return businessName;
    }
    private String genertindustrycode(){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 5);
        String str = "CIS1"+code;
        return str;
    }
    private String genertfinalcode(boolean flag){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 3);
        String code2 = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 1);

        String str = "Z300"+code2+code;
        if(flag){
            return "Z300"+code2;
        }
        return str;
    }
    //生成公司名字
    private String generBusinessName(){
        Random r = new Random();


        String citys[] = {"北京","广东","山东","江苏","河南","上海","河北","浙江","香港","山西","陕西","湖南","重庆","福建","天津","云南","四川","广西","安徽","海南","江西","湖北","辽宁","内蒙古"};
        //int randomInt = r.nextInt(10000)%citys.length;
        int randomInt = r.nextInt(23);
        String businessName = citys[randomInt]+randowText(3)+"有限公司";
        return businessName;
    }
    //生成公司名字share
    private String generBusinessName_share(){
        Random r = new Random();
        String citys[] = {"北京","广东","山东","江苏","河南","上海","河北","浙江","香港","山西","陕西","湖南","重庆","福建","天津","云南","四川","广西","安徽","海南","江西","湖北","辽宁","内蒙古"};
        //int randomInt = r.nextInt(10000)%citys.length;
        int randomInt = r.nextInt(23);
        String businessName = citys[randomInt]+randowText(4)+"股份有限公司";
        return businessName;
    }
    //省份代表处
    private String genertProvience(boolean flag){
        Random r = new Random();
        String citys[] = {"北京","广东","山东","江苏","河南","上海","河北","浙江","香港","山西","陕西","湖南","重庆","福建","天津","云南","四川","广西","安徽","海南","江西","湖北","辽宁","内蒙古"};
        //int randomInt = r.nextInt(10000)%citys.length;
        int randomInt = r.nextInt(23);
        if (flag){
            return citys[randomInt]+"代表处";
        }
        return citys[randomInt];
    }
    //省份
//    private String genertProvience(){
//        Random r = new Random();
//        String citys[] = {"北京","广东","山东","江苏","河南","上海","河北","浙江","香港","山西","陕西","湖南","重庆","福建","天津","云南","四川","广西","安徽","海南","江西","湖北","山西","辽宁","内蒙古"};
//        int randomInt = r.nextInt(10000)%citys.length;
//        return citys[randomInt];
//    }


    //生成accat_code
    private String genertAccatCode(){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 5);
        String str = "REP_AR_"+code;
        return str;
    }

    //生成customer_code
    private String genertCustomerCode(){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 7);
        String str = "C"+code;
        return str;
    }

    //生成sales_contract_no
    private String genertSalesContractNo(){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(1, 12);
        //生成大写字母
        Random random = new Random();
        char ranUpLetter = (char) ((random.nextInt(26) + 65));
        String str = "CY"+code+ranUpLetter;
        return str;
    }

    //生成有前缀字母的随机数字
    private String BUcode(String strs){
        //String st = strs.substring(0,2);
        //String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 2);
        String str = "BU00"+strs;
        return str;
    }
    private String genertPeriodId(){
//        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 2);
//
//        String str = "2022"+code;
//        return str;
        int minYear = 2000; // 设置最小年份
        int maxYear = 2025; // 设置最大年份
        int year = ThreadLocalRandom.current().nextInt(minYear, maxYear + 1); // 随机生成年份

        int month = ThreadLocalRandom.current().nextInt(1, 13); // 随机生成月份
        String yearMonth = String.format("%d%02d", year, month); // 格式化输出
        return yearMonth;
    }

    //生成指定位数的随机数字
    private String genertUnNum(int size){
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, size);

        return code;
    }

    //生成随机数字
    private BigDecimal genertNum(){
        Random random = new Random();

        BigDecimal bigDecimal = BigDecimal.valueOf(random.nextInt());
        return bigDecimal;
    }

    //生成随机小数字
    private BigDecimal genertLettleNum( int num){
        Random random = new Random();

        BigDecimal bigDecimal = BigDecimal.valueOf(random.nextInt(num));
        return bigDecimal;
    }
    //生成指定长度的随机汉字
    private String randowText(int size){
        String strs = "";
        for(int i = 0;i<size;i++){
            char ch = GenerText();
            strs += ch;
        }
        return strs;
    }
    private char GenerText() {
        String str = "";
        int hightPos; //
        int lowPos;

        Random random = new Random();

        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hightPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("错误");
        }


        return str.charAt(0);
    }
}
